﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using com.hyrentals.dbaccess;
using com.hyrentals.data;
using com.hyrentals.common;
using System.Web;
using System.Security.Cryptography;
using System.IO;

namespace com.hyrentals.logic
{
    public class AccountLogic
    {
        private AccountDBAccess _accountDBAccess;

        public AccountLogic()
        {
            _accountDBAccess = new AccountDBAccess();
        }
        public UserBasic ValidateUser(string account)
        {
            return _accountDBAccess.GetUserBasicByEmail(account);
        }

        public bool ValidatePassword(string password, UserBasic user)
        {
            string EncodeMD5Password = EncodeLib.MD5String(password);
            return EncodeMD5Password == user.Password ? true : false;
        }

        public void SetAuthCookie(string userId, bool bRememberMe)
        {
            FormsAuthentication.SetAuthCookie(userId, bRememberMe);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        public bool HasEmailRegistered(string email)
        {
            return _accountDBAccess.GetUserCountByEmail(email) > 0;
        }

        public bool InsertUser(string userId, string firstName, string lastName, string email, string password)
        {
            string encodePassword = EncodeLib.MD5String(password);
            return _accountDBAccess.InsertUser(
                userId, encodePassword, firstName, lastName, GenderTypeEnum.Unknown, email, 
                EmailStatusEnum.Unverified, AccountStatusEnum.Normal, DateTime.UtcNow);
        }

        public UserBasic GetUser(string userId)
        {
             UserBasic user = _accountDBAccess.GetUserBasicById(userId);
             if (user == null || user.AccountStatus == AccountStatusEnum.Disable)
                 return null;
             else
                 return user;
        }
    }
}
